#!/bin/bash

      #//////////////////////////////////////////////////////
      #
      #  Betsu-Banner v0.1.4 for the Betsu Project
      #  Copyright (C) 2022 Alexa Hattori <unixcat@anche.no)
      #  SPDX-License-Identifier: GPL-3.0-only
      #  <https://gnu.org/license/gpl-3.0.html>
      #
      #//////////////////////////////////////////////////////

# Variables
version="v0.1.4"

source $HOME/.config/betsu/banner.cfg

generate_config () {
    if [ -d "$HOME/.config/betsu" ]; then # Check if $HOME/.config/betsu exist
	echo "0" > /dev/null # Do Nothing
    else
	echo "[ INFO ] Betsu-Banner |" $HOME/.config/betsu "doesn't exists, creating..."  
	mkdir -v $HOME/.config/betsu # Create directory for Betsu configuration files
    fi

    echo "[ INFO ] Betsu-Banner | Generating the Configuration File..."

    echo " 
    # Betsu-Banner $version Configuration File
    # $HOME/.config/betsu/banner.cfg

    # Header Settings
    
    date_format="12" # 12-hour or 24-hour.

    ascii_betsu_logo='false'

    # First Block

    cpu_temp='true'
    ram_usage='true'
    swap_usage='false'
    battery='false'
    uptime='true'

    # storage

    rootfs='true'
    userspace='true'
    ## Custom
    
    enable_custom_mountpoint='false'
    custom_mountpoint='REPLACE_ME' 

    # Second Block    

    # Network

    lan_ip='true'
    wan_ip4='false'
    wan_ip6='false'

    # Other

    update_interval='1' # In seconds, for --loop option" > $HOME/.config/betsu/banner.cfg
}

if [ -f "$HOME/.config/betsu/banner.cfg" ] # Check is configuration file exist, if no - run generate_config
then
    echo "0" > /dev/null
else
    echo "[ WARNING ] Betsu-Banner | Configuration File doesn't exist, running generate_config function."
    generate_config
    echo "[ INFO ] Betsu-Banner | Please, run betsu-banner again."
    exit
fi

print_line () { # Line
    echo "====================================================="
}

print_header () { # Prints "Betsu Banner vX.X.X : Date"
    if [[ $date_format == *"12"* ]];
    then
	echo "Betsu Banner" $version ":" $(date +%r) "-" $(date +%D)
    elif [[ $date_format == *"24"* ]];
	 then
	     echo "Betsu Banner" $version ":" $(date +%R) "-" $(date +%D)
	 else
	     echo "[ FAILURE ] Configuration Error: unknown option"
	     echo "Variable: date_format"
	     echo "Available options: 12, 24"
	     echo "[ DEBUG ] Stopped at print_header"
	     exit;
	 fi
}

print_betsu_logo () { # Prints ASCII Betsu Logo
    echo "
   _            _               
  | |__    ___ | |_  ___  _   _
  | '_ \  / _ \| __|/ __|| | | |
  | |_) ||  __/| |_ \__ \| |_| |
  |_.__/  \___| \__||___/ \__,_|
  
  "
}

print_battery () { # Battery percentage, + status
    echo "-" $(acpi | sed 's/Battery 0:/Battery is/')
}

print_cpu () { # CPU Temperature
    echo "- CPU Temp :" $(sed 's/000$/C/' /sys/class/thermal/thermal_zone0/temp)
}

print_lan_ip () { # Local LAN IP Address
    echo "- LAN IP :" $(hostname -I | sed 's/ .*//')
}

print_wan_ip4 () { # Public ipv4 Address
    echo "- WAN IP :" $(curl -s --ipv4 ident.me)
}

print_wan_ip6 () { # Public ipv6 Address
    echo "- WAN IP :" $(curl -s --ipv6 ident.me)
}

print_ram () { # RAM Usage
    echo "- RAM Usage :" $(free --mega | awk '(NR==2){ print $3 }') "Mib" "/" $(free --mega | awk '(NR==2){ print $2 }') "Mib"
}

print_swap () { # Swap Usage
    echo "- SWAP Usage :" $(free --mega | awk '(NR==3){ print $3 }') "Mib" "/" $(free --mega | awk '(NR==3){ print $2 }') "Mib"
}

print_storage_rootfs () { # rootfs ( / ) disk usage: used/total
    echo "- RootFS :" $(df -h / | awk '(NR==2){ print $3 }' | sed 's/G/ Gib/' | sed 's/M/ Mib/') \
	 "/" $(df -h / | awk '(NR==2){ print $2 }' | sed 's/G/ Gib/' | sed 's/M/ Mib/')   
}

print_storage_userspace () { # userspace ( /home ) disk usage: used/total
    echo "- Userspace :" $(df -h /home | awk '(NR==2){ print $3 }' | sed 's/G/ Gib/' | sed 's/M/ Mib/') \
	 "/" $(df -h /home | awk '(NR==2){ print $2 }' | sed 's/G/ Gib/' | sed 's/M/ Mib/')
}

print_storage_custom () {
    echo "- Custom Storage :" $(df -h $custom_mountpoint | awk '(NR==2){ print $2 }' | sed 's/G/ Gib/' | sed 's/M/ Mib/') \
	 "/" $(df -h $custom_mountpoint | awk '(NR==2){ print $2 }' | sed 's/G/ Gib/' | sed 's/M/ Mib/')
}

print_uptime () {
    echo "- Uptime :" $(uptime -p) 
}

print_once () { # Print all information what is enabled in configuration file.
    print_line # Line
    print_header
    print_line # Line
    
    if [[ $ascii_betsu_logo == *"true"* ]];
    then
	print_betsu_logo
    else
	echo ""
    fi

    if [[ $uptime == *"true"* ]]; 
    then
	print_uptime
    else 
	echo "0" > /dev/null
    fi

    if [[ $cpu_temp == *"true"* ]]; # if "cpu_temp" in configuration file = "true", print CPU Temperature.
    then
	print_cpu
    else
	echo "0" > /dev/null
    fi

    if [[ $ram_usage == *"true"* ]];
    then
	print_ram
    else
	echo "0" > /dev/null
    fi

    if [[ $swap_usage == *"true"* ]];
    then
	print_swap
    else
	echo "0" > /dev/null
    fi

    if [[ $battery == *"true"* ]];
    then
	print_battery
    else
	echo "0" > /dev/null
    fi
    
#    if [[ $cpu_temp == *"false"* ]] && [[ $ram_usage == *"false"* ]] && [[ $battery == *"false"* ]];
#    then
#	echo "0" > /dev/null
#    else
#	print_line
#    fi

    if [[ $rootfs == *"true"* ]];
    then
	print_storage_rootfs
    else
	echo "0" > /dev/null
    fi

    if [[ $userspace == *"true"* ]];
    then
	print_storage_userspace
    else
	echo "0" > /dev/null
    fi

    if [[ $enable_custom_mountpoint == *"true"* ]];
    then
	print_storage_custom
    else
	echo "0" > /dev/null
    fi
    
    echo ""
    print_line

    if [[ $lan_ip == *"true"* ]];
    then
	print_lan_ip
    else
	echo "0" > /dev/null
    fi

    if [[ $public_ip4 == *"true"* ]];
    then
	print_wan_ip4
    else
	echo "0" > /dev/null
    fi

    if [[ $public_ip6 == *"true"* ]];
    then
	print_wan_ip6
    else
	echo "0" > /dev/null
    fi
    
    print_line   

#    if [[ $lan_ip == *"false"* ]] && [[ $public_ip4 == *"false"* ]] && [[ $public_ip6 == *"false" ]] 
#    then
#	echo "0" > /dev/null
#    else
#	print_line
#    fi

    echo ""
}

print_looped () { # Loop print_once, option: --loop (or -l) 
    while true
    do
	clear
	print_once
	sleep $update_interval # Update interval in seconds, reading $update_interval from configration file.
    done
}

print_help () {
    echo "usage:"
    echo "--help (-h) : print this"
    echo "--loop (-l) : loop output"
    echo "--generate-config (-gc) : Generate Configuration File"
}

# Console Arguments
case $1 in
    "--help" )
	print_help
	;;
    "-h" )
	print_help
	;;
    "--loop" )
	print_looped
	;;
    "-l" )
	print_looped
	;;
    "--generate-config" )
	generate_config
	;;
    "-gc" )
	generate_config
	;;
    * )
	print_once
esac
